package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 平台管理
 */
@Api(tags = "平台管理中心")
@RestController
@RequestMapping("/admin/product")
public class ManageController {


    @Autowired
    private ManageService manageService;

    //查询一级分类集合
    @ApiOperation("查询一级分类集合")
    @GetMapping("getCategory1")
    public Result getCategory1(){
        List<BaseCategory1> baseCategory1List =  manageService.getCategory1();
        return Result.ok(baseCategory1List);
    }
    //查询二级分类集合 根据一级分类的选择
    @ApiOperation("查询二级分类集合 根据一级分类的选择")
    @GetMapping("/getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable(name = "category1Id") Long category1Id){
        List<BaseCategory2> baseCategory2List =  manageService.getCategory2(category1Id);
        return Result.ok(baseCategory2List);
    }
    @GetMapping("getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable(name = "category2Id") Long category2Id){
        List<BaseCategory3> baseCategory3List =  manageService.getCategory3(category2Id);
        return Result.ok(baseCategory3List);
    }
    //当用户选择一级分类时 查询一级分类对应的平台属性
    //当用户选择二级分类时 查询一级分类及二级分类对应的平台属性
    //当用户选择三级分类时 查询一级分类及二级分类、三级分类 对应的平台属性
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(
            @PathVariable(name = "category1Id") Long category1Id,
            @PathVariable(name = "category2Id") Long category2Id,
            @PathVariable(name = "category3Id") Long category3Id

    ){

        List<BaseAttrInfo> baseAttrInfoList =  manageService.attrInfoList(category1Id,category2Id,category3Id);

        return Result.ok(baseAttrInfoList);
    }

    //保存平台属性
    @PostMapping("saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){

        manageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }
    @GetMapping("/getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable(name = "attrId") Long attrId){

        List<BaseAttrValue> attrValueList =  manageService.getAttrValueList(attrId);
        return Result.ok(attrValueList);
    }

    //根据三级分类 ID  查询商品集合  （商品过多 可以分页)
    @GetMapping("{page}/{limit}")
    public Result getSpuInfoByPage(@PathVariable(name = "page") Integer page,
                                   @PathVariable(name = "limit") Integer limit,
                                   Long category3Id
    ){
        IPage<SpuInfo> spuInfoPage =  manageService.getSpuInfoByPage(page,limit,category3Id);
        return Result.ok(spuInfoPage);
    }

    @ApiOperation("查询所有平台销售属性")
    @GetMapping("baseSaleAttrList")
    public Result baseSaleAttrList(){
        List<BaseSaleAttr> baseSaleAttrList =  manageService.baseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }
    @ApiOperation("查询所有平台销售属性")
    @GetMapping("baseTrademark/getTrademarkList")
    public Result getTrademarkList(){
        List<BaseTrademark> baseTrademarkList =  manageService.getTrademarkList();
        return Result.ok(baseTrademarkList);
    }

    //保存spu
    @ApiOperation("保存Spu")
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){

        manageService.saveSpuInfo(spuInfo);

        return Result.ok();
    }

    @ApiOperation("查询Sku列表")
    @GetMapping("list/{page}/{limit}")
    public Result skuList(@PathVariable(name = "page") Integer page,
                          @PathVariable(name = "limit") Integer limit){
        IPage<SkuInfo> skuInfoIpage =  manageService.skuList(page,limit);

        return Result.ok(skuInfoIpage);
    }

    @ApiOperation("根据spuId获取销售属性")
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable(name = "spuId") Long spuId){

        List<SpuSaleAttr> spuSaleAttrList = manageService.spuSaleAttrList(spuId);

        return Result.ok(spuSaleAttrList);
    }

    @ApiOperation("根据spuId获取图片列表")
    @GetMapping("spuImageList/{spuId}")
    public Result spuImageList(@PathVariable(name = "spuId") Long spuId){

        List<SpuImage> spuImageList = manageService.spuImageList(spuId);

        return Result.ok(spuImageList);
    }
    @PostMapping("saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){

        manageService.saveSkuInfo(skuInfo);

        return Result.ok();
    }

    //
    @GetMapping("onSale/{skuId}")
    public Result onSale(@PathVariable(name = "skuId") Long skuId){

        manageService.onSale(skuId);

        return Result.ok();
    }
    @GetMapping("cancelSale/{skuId}")
    public Result cancelSale(@PathVariable(name = "skuId") Long skuId){
        manageService.cancelSale(skuId);

        return Result.ok();
    }


}
